package com.changgou.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/** 秒杀的监听 */
@Component
@RabbitListener(queues = "${mq.pay.queue.seckillorder}")
public class SeckillMessageListener {
@Autowired
private SeckillOrderService seckillOrderService;
  @RabbitHandler
  public void getMessage(String message) {
    System.out.println("秒杀用户" + message);
    Map<String, String> map = JSON.parseObject(message, Map.class);
    // 通信标识
    String return_code = map.get("return_code");
    if (return_code.equals("SUCCESS")) {
      // 业务结果
      String result_code = map.get("result_code");
      // 订单号
      String outTradeNo = map.get("out_trade_no");

      if (result_code.equals("SUCCESS")) {
        // 修改订单的状态
          seckillOrderService.updatePayStatus("mayun", outTradeNo);
      } else {
        //删除订单
          seckillOrderService.deleteOrder("mayun");
      }
    }
  }
}
